import { Scene } from '@babylonjs/core/scene';
import { BabylonContainer } from '../base/babylon-container';
import { Texture } from '@babylonjs/core/Materials/Textures/texture';


/**
 * 画布纹理参数对象
 * @param generateMipMaps 创建分级纹理
 * @param samplingMode 采样模式
 * @param format 格式
 * @param invertY 翻转y向
 * @param width 画布宽（像素）
 * @param height 画布高（像素）
 * @param canvas 画布
 */
interface ICanvasTextureOptions {
    generateMipMaps?: boolean,
    samplingMode?: number,
    format?: number,
    invertY?: boolean,
    width?: number,
    height?: number,
    canvas?: HTMLCanvasElement
}


/**
 * 画布纹理基类
 * @param name 名称
 * @param options 参数对象
 * @param scene 所在场景
 */
abstract class CanvasTextureBase {

    /**
     * 生成的纹理
     */
    public readonly texture: Texture;

    constructor(name: string, options?: ICanvasTextureOptions, scene?: Scene) {
        const ops = options || {};
        this.initialzeVariable();
        const canvas = this.drawCanvas(ops);
        this.texture = new Texture(canvas.toDataURL(), scene, ops.generateMipMaps, ops.invertY, ops.samplingMode, undefined, undefined, undefined, undefined, ops.format);
        this.texture.vAng = BabylonContainer.isRightHand ? 3.1415926 : 0;
        this.texture.name = name;
    }

    /**
     * 初始化变量
     */
    protected abstract initialzeVariable(): void;

    /**
     * 绘制图形
     * @param options 参数对象
     * @returns 画布元素
     */
    protected abstract drawCanvas(options: ICanvasTextureOptions): HTMLCanvasElement;

}


export { CanvasTextureBase };
export type { ICanvasTextureOptions };